/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： mysite
 * 文件名称： articleclassify.js
 * Date：18-9-28 下午3:02
 * Author: guomw
 */

$(function () {
    classifyHandler.init();
});


var classifyHandler = {
    localData: null,
    params: {
        keyword: "",
        pageIndex: 1
    },
    templateList: $("#table-content-template").html(),
    init: function () {
        var self = this;
        hotUtil.loading.show();
        hotUtil.ajaxCall("/article/ajax/classifylist", self.params, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                var data = ret.data;
                self.localData = data.list;
                var appendHtml = '';

                $.each(data.list, function (i, item) {
                    var tempHtml = self.templateList;
                    tempHtml = tempHtml.replace(/{index}/gm, i + 1);
                    tempHtml = tempHtml.replace(/{classifyId}/gm, item.classifyId);
                    tempHtml = tempHtml.replace(/{title}/gm, item.classifyTitle);
                    appendHtml += tempHtml;
                });
                $("#table-content").html(appendHtml);
                var pageinate = new hotUtil.paging(".pagination", self.params.pageIndex, 20, data.pageCount, data.totalCount, 7);
                pageinate.init(function (p) {
                    goTo(p, function (page) {
                        self.params.pageIndex = page;
                        self.init();
                    });
                });
            }
        })
    },
    getModel: function (classifyId) {
        var result = {};
        $.each(this.localData, function (i, item) {
            if (item.classifyId === classifyId) {
                result = item;
                return false;
            }
        });
        return result;
    },
    edit: function (dataId) {
        var classifyTitle = "";
        if (dataId > 0) {
            var data = this.getModel(dataId);
            if (data != null) {
                classifyTitle = data.classifyTitle
            }
            else {
                dataId = 0;
            }
        }
        $("#classifyId").val(dataId);
        $("#classifyTitle").val(classifyTitle);
    },
    delete: function (dataId) {
        var self = this;
        swal({
            title: "您确定要删除这条信息吗",
            text: "删除后将无法恢复，请谨慎操作！",
            type: "warning",
            showCancelButton: true,
            confirmButtonColor: "#DD6B55",
            confirmButtonText: "删除",
            closeOnConfirm: false
        }, function () {
            hotUtil.loading.show();
            hotUtil.ajaxCall("/article/ajax/deleteclassify", {
                classifyId: dataId
            }, function (ret) {
                hotUtil.loading.close();
                if (ret.resultCode === 2000) {
                    self.init();
                    swal("删除成功！", "您已经永久删除了这条信息。", "success");
                }
                else {
                    swal("删除失败！", ret.resultMsg, "error");
                }
            });
        });
    },
    btnSubmit: function () {
        var self = this;
        if (hotUtil.isNullOrEmpty($("#classifyTitle").val())) {
            swal("请输入分类名称", "", "warning")
            $("#classifyTitle").focus();
            return false;
        }
        hotUtil.loading.show();
        hotUtil.ajaxCall("/article/ajax/editclassify", {
            classifyId: parseInt($.trim($("#classifyId").val())),
            classifyTitle: $.trim($("#classifyTitle").val())
        }, function (ret) {
            hotUtil.loading.close();
            if (ret.resultCode === 2000) {
                $('#addArticleClassifyModal').modal('hide');
                self.init();
                swal(ret.resultMsg, "", "success");
            }
            else {
                swal(ret.resultMsg, "", "error")
            }
        });
    }
}